package com.cheng.frame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

import com.cheng.listener.CheckInfoListener;
import com.cheng.util.DataUtil;



public class InputInfoFrame extends JFrame{
	private static final long serialVersionUID = 1L;
	private static InputInfoFrame frame;
	private PerInfoPanel perInfoPanel;
	private PriInfoPanel priInfoPanel;
	private CardInfoPanel cardInfoPanel;
	private JPanel jPanel3;
	private JButton bLast;
	private JButton bNext;
	private static final String PREFERRED_LOOK_AND_FEEL = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";
	private InputInfoFrame() {
		frame = this;
		initComponents();
		frame.setDefaultCloseOperation(InputInfoFrame.EXIT_ON_CLOSE);
		frame.setTitle("火车票实名认证系统");
		frame.getContentPane().setPreferredSize(frame.getSize());
		frame.pack();
		frame.setLocationRelativeTo(null);
	}
	
	public static InputInfoFrame instance(){
		if(frame == null)
			frame = new InputInfoFrame();
		return frame;
	}

	private void initComponents() {
		setTitle("火车票实名认证系统");
		setLayout(new GroupLayout());
		perInfoPanel = new PerInfoPanel().getPanel();
		priInfoPanel = new PriInfoPanel().getPanel();
		cardInfoPanel = new CardInfoPanel().getPanel();
		add(perInfoPanel, new Constraints(new Leading(20, 370, 10, 10), new Leading(20, 320, 10, 10)));
		add(cardInfoPanel, new Constraints(new Leading(410, 370, 10, 10), new Leading(20, 480, 10, 10)));
		add(priInfoPanel, new Constraints(new Leading(20, 370, 10, 10), new Leading(350, 150, 10, 10)));
		add(getJPanel3(), new Constraints(new Leading(410, 370, 10, 10), new Leading(500, 100, 10, 10)));
		setSize(800, 600);
		setIconImage(DataUtil.instance().getIcon());
		setResizable(false);
		if(LoadingFrame.instance().isVisible())
			LoadingFrame.instance().dispose();
	}
	
	private JButton getBNext() {
		if (bNext == null) {
			bNext = new JButton();
			bNext.setText("下一步");
			bNext.setDefaultCapable(true);
			bNext.addActionListener(new CheckInfoListener(perInfoPanel.getTName(),perInfoPanel.getTSex(),perInfoPanel.getTPlace(),
					perInfoPanel.getTNation(),perInfoPanel.getTBirth(),cardInfoPanel.getTSchool(),cardInfoPanel.getTSnum(),
					cardInfoPanel.getTSyear(),cardInfoPanel.getTScardID(),cardInfoPanel.getTRemark(),priInfoPanel.getTQuestion(),
					priInfoPanel.getTAnswer(),bNext));
		}
		return bNext;
	}

	private JButton getBLast() {
		if (bLast == null) {
			bLast = new JButton();
			bLast.setText("退出");
			bLast.setDefaultCapable(true);
			bLast.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					if(e.getSource() == bLast){
						System.exit(0);
					}
				}
			});
		}
		return bLast;
	}

	private JPanel getJPanel3() {
		if (jPanel3 == null) {
			jPanel3 = new JPanel();
			jPanel3.setLayout(new GroupLayout());
			jPanel3.add(getBNext(), new Constraints(new Leading(60, 100, 10, 10), new Leading(25, 50, 10, 10)));
			jPanel3.add(getBLast(), new Constraints(new Leading(220, 100, 10, 10), new Leading(25, 50, 10, 10)));
		}
		return jPanel3;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				InputInfoFrame frame = InputInfoFrame.instance();
				frame.setDefaultCloseOperation(InputInfoFrame.EXIT_ON_CLOSE);
				frame.setTitle("火车票实名认证系统");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}

}
